package com.prd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prd.dto.CustomerRemarkDTO;
import com.prd.mapper.CustomerRemarkMapper;
import com.prd.model.CustomerRemarkDO;
import com.prd.service.CustomerRemarkService;
import com.prd.tools.CopyNonNullBeanUtils;
import com.prd.util.JsonData;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 客户-备注总表 服务实现类
 * </p>
 *
 * @author o1
 * @since 2023-11-24
 */
@Service("CustomerRemarkService")
public class CustomerRemarkServiceImpl extends ServiceImpl<CustomerRemarkMapper, CustomerRemarkDO> implements CustomerRemarkService {

    @Autowired
    private CustomerRemarkMapper remarkMapper;

    @Override
    public JsonData uAdd(CustomerRemarkDTO remarkDTO) {
        CustomerRemarkDO remarkDO = new CustomerRemarkDO();
        remarkDTO.setCreateDatetime(new Date());
        BeanUtils.copyProperties(remarkDTO, remarkDO);
        remarkMapper.insert(remarkDO);
        return JsonData.buildSuccess(remarkDO);
    }

    @Override
    public JsonData addSimple(CustomerRemarkDO remarkDO) {
        CustomerRemarkDO remarkDB = new CustomerRemarkDO();
        remarkDO.setCreateDatetime(new Date());
        BeanUtils.copyProperties(remarkDO, remarkDB);
        remarkMapper.insert(remarkDB);
        return JsonData.buildSuccess(remarkDB);
    }

    @Override
    public JsonData uUpdateAndAdd(CustomerRemarkDTO remarkDTO) {
        CustomerRemarkDO remarkDO = remarkMapper.selectById(remarkDTO.getId());
        if (remarkDO != null) {
            CopyNonNullBeanUtils.copyProperties(remarkDTO, remarkDO);
            remarkMapper.updateById(remarkDO);
        }
        else {
            remarkDO = new CustomerRemarkDO();
            remarkDTO.setCreateDatetime(new Date());
            BeanUtils.copyProperties(remarkDTO, remarkDO);
            remarkMapper.insert(remarkDO);
        }
        return JsonData.buildSuccess(remarkDO);
    }

    @Override
    public JsonData update(CustomerRemarkDO remarkDO) {
        CustomerRemarkDO remarkDB = remarkMapper.selectById(remarkDO.getId());
        if (remarkDB != null) {
            CopyNonNullBeanUtils.copyProperties(remarkDO, remarkDB);
            remarkMapper.updateById(remarkDB);
            return JsonData.buildSuccess(remarkDO);
        }
        else
            return JsonData.buildError("没有找到对应备注");
    }

    @Override
    public Map<String, Object> uSearch(int page, int size, int type, String projectId, String customerNo) {
        Page<CustomerRemarkDO> pageInfo = new Page<>(page, size);
        LambdaQueryWrapper<CustomerRemarkDO> wrapper = new LambdaQueryWrapper<>();
        if (!Objects.equals(projectId, "")) {
            wrapper.eq(CustomerRemarkDO::getNo, projectId);
        }
        else if (customerNo != null)
            wrapper.eq(CustomerRemarkDO::getNo, customerNo);
        if (type != -1)
            wrapper.eq(CustomerRemarkDO::getType, type);
        IPage<CustomerRemarkDO> customerDOIPage = remarkMapper.selectPage(pageInfo, wrapper);
        Map<String, Object> map = new HashMap<>(3);
        map.put("total_record", customerDOIPage.getTotal());
        map.put("total_page", customerDOIPage.getPages());
        map.put("current_data", customerDOIPage.getRecords().stream().map(this::doToDto).collect(Collectors.toList()));
        return map;
    }

    @Override
    public List<CustomerRemarkDTO> uSearchForList(String no, int type) {
        LambdaQueryWrapper<CustomerRemarkDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CustomerRemarkDO::getNo, no);
        wrapper.eq(CustomerRemarkDO::getType, type);
        return remarkMapper.selectList(wrapper).stream().map(this::doToDto).collect(Collectors.toList());
    }

    private CustomerRemarkDTO doToDto(CustomerRemarkDO remarkDO) {
        CustomerRemarkDTO remarkDTO = new CustomerRemarkDTO();
        BeanUtils.copyProperties(remarkDO, remarkDTO);
        if (remarkDO.getFileUrl() != null && remarkDO.getFileUrl().indexOf(",") != 0)
            remarkDTO.setFileUrlList(Arrays.asList(remarkDO.getFileUrl().split(",")));
        else
            remarkDTO.setFileUrlList(Arrays.asList(remarkDO.getFileUrl()));
        if (remarkDO.getImgUrl() != null && remarkDO.getImgUrl().indexOf(",") != 0)
            remarkDTO.setImgUrlList(Arrays.asList(remarkDO.getImgUrl().split(",")));
        else
            remarkDTO.setImgUrlList(Arrays.asList(remarkDO.getImgUrl()));
        if (remarkDO.getRecordUrl() != null && remarkDO.getRecordUrl().indexOf(",") != 0)
            remarkDTO.setRecordUrlList(Arrays.asList(remarkDO.getRecordUrl().split(",")));
        else
            remarkDTO.setRecordUrlList(Arrays.asList(remarkDO.getRecordUrl()));
        return remarkDTO;
    }
}
